package cn.driver.car.controller;

import cn.driver.car.service.CarService;
import cn.driver.common.pojo.Car;
import cn.driver.common.tools.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 车辆控制层
 */

@RestController
@RequestMapping(value = "/car")
@CrossOrigin
@Api(tags = "car")
@Slf4j
public class Carcontroller {


    @Autowired
    private CarService carService;

    @ApiOperation(value = "查询所有")
    @GetMapping("/*")
    public ResponseData getCarList() {
        List<Car> list = carService.getCarList();
        try {
            if (list.size() > 0) {
                return ResponseData.ok(list);
            } else {
                return ResponseData.error();
            }
        } catch (Exception e) {
            return ResponseData.error("发生错误:" + e.getMessage() + " 请联系管理员处理");
        }
    }

    @ApiOperation(value = "添加")
    @PostMapping("/addCar")
    public ResponseData addCar(@RequestBody Car car) {
        try {
            int num = carService.addCar(car);
            if (num > 0) {
                return ResponseData.ok_201();
            } else {
                return ResponseData.fail_201();
            }
        } catch (Exception e) {
            return ResponseData.error("发生错误:" + e.getMessage() + " 请联系管理员处理");
        }
    }

    @ApiOperation(value = "修改")
    @PutMapping("/updateCar")
    public ResponseData updataCar(@RequestBody Car car) {
        try {
            int num = carService.updateCar(car);
            if (num > 0) {
                return ResponseData.ok_201();
            } else {
                return ResponseData.fail_201();
            }
        } catch (Exception e) {
            return ResponseData.error("发生错误:" + e.getMessage() + " 请联系管理员处理");
        }
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/deleteCar/{carNo}")
    public ResponseData deleteCar(@PathVariable String carNo) {
        try {
            int num = carService.deleteCar(carNo);
            if (num > 0) {
                return ResponseData.ok_201();
            } else {
                return ResponseData.fail_201();
            }
        } catch (Exception e) {
            return ResponseData.error("发生错误:" + e.getMessage() + " 请联系管理员处理");
        }

    }

    @ApiOperation(value = "分页")
    @GetMapping("/pageList")
    public ResponseData pageList(@RequestParam Integer Num,@RequestParam  Integer Size) {
        List<Car> list = carService.getList(Num, Size);
        try {
            if (list.size() > 0) {
                return ResponseData.ok(list);
            } else {
                return ResponseData.ok(null, "没有找到数据!");
            }
        } catch (Exception e) {
            return ResponseData.error("发生错误:" + e.getMessage() + " 请联系管理员处理");
        }

    }

    @ApiOperation(value = "根据车牌号模糊查询车辆")
    @GetMapping("/mohuList")
    public ResponseData mohuList(@RequestParam String carNo) {
        List<Car> list = carService.mohuList(carNo);
        try {
            if (list.size() > 0) {
                return ResponseData.ok(list);
            } else {
                return ResponseData.ok(null, "没有找到数据!");
            }
        } catch (Exception e) {
            return ResponseData.error("发生错误:" + e.getMessage() + " 请联系管理员处理");
        }
    }
}
